package com.linzelin.config;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String uri = request.getRequestURI();
        //获取路径
        System.out.println(uri);
        if ("/Login/login".equals(uri) || "/index.jsp".equals(uri) || "/Login/goLogin".equals(uri)) {
            //如果是登录相关的就直接放行即可
            return true;
        }
        if (request.getSession().getAttribute("user") != null) {
            //如果有对应的user数据就直接放行即可
            return true;
        }
        request.getRequestDispatcher("/Login/goLogin").forward(request, response);
        //没有登录就直接踢回登录界面
        return false;
    }
}
